﻿package top.yangyitao.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class RegisterInterceptor implements Interceptor {

	private static final long serialVersionUID = 1L;
	private String errorMessage;
	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		ActionContext actionContext = ActionContext.getContext();
		HttpServletRequest httpServletRequest = (HttpServletRequest)actionContext.get(ServletActionContext.HTTP_REQUEST);
		String userName = httpServletRequest.getParameter("user.userName");
		String password = httpServletRequest.getParameter("user.password");
		String email = httpServletRequest.getParameter("user.email");
		String checkPassword = httpServletRequest.getParameter("checkPassword");
		if(userName.trim().length()==0) {
			errorMessage = "*用户名为空";
			actionContext.put("errorMessage", errorMessage);
			return "error";			
		}
		else if(password.trim().length()==0) {
			errorMessage = "*密码为空";
			actionContext.put("errorMessage", errorMessage);
			return "error";	
		}
		else if(userName.length()<3||password.length()<6) {
			errorMessage = "*用户名长度不小于3，密码长度不小于6";
			actionContext.put("errorMessage", errorMessage);
			return "error";
		}
		else if(email.trim().length()==0) {
			errorMessage = "*邮箱为空";
			actionContext.put("errorMessage", errorMessage);
			return "error";
		}
		else if(!password.equals(checkPassword)) {
			errorMessage = "*两次输入的密码不相同";
			actionContext.put("errorMessage", errorMessage);
			return "error";
		}
		actionInvocation.invoke();
		return null;
	}

	@Override
	public void destroy() {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void init() {
		// TODO 自动生成的方法存根
		
	}

}
